﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace QuaManSys.Rights
{
    public partial class RolesDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strinfo.Visible = false;
            if (!IsPostBack)
            {
                if (Request.QueryString["Id"] != null)
                {
                    GetRole(Request.QueryString["Id"].ToString());
                }

            }
        }

        protected void GetRole(string id)
        {
            lblId.Text = id;

            using (var entity = new QuaManSysEntities())
            {
                var queryRole = from q in entity.SysRole
                                where q.Id == id
                                select q;

                SysRole aRole = queryRole.Single();
                txtName.Text = aRole.Name;
                txtDescription.Text = aRole.Description;
                lblCreateTime.Text = aRole.CreateTime.ToString();
                lblCreatePerson.Text = aRole.CreatePerson;
                lblUpdateTime.Text = aRole.UpdateTime.ToString();
                lblUpdatePerson.Text = aRole.UpdatePerson;

                //var queryRoleUsers = from p in entity.Tab_Person
                //                    join q in entity.SysRoleSysPerson
                //                    on p.employeeId equals q.SysPersonId
                //                    join r in entity.SysRole
                //                    on q.SysRoleId equals r.Id
                //                    where r.Id == id
                //                    select p.employeeId;


                //List<string> rid = queryRoleUsers.ToList<string>();
                //int n = rid.Count;
                //string strUsers = "";
                //if (n != 0)
                //{
                //    string[] str = new string[n];
                //    int i = 0;
                //    foreach (var item in rid)
                //    {
                //        str = rid[i].ToString().Split(',');
                //        i++;
                //    }
                //    strUsers = StringArrayToString(str, '|');
                //}
                //else
                //{
                //    strUsers = "您暂没有为该用户分配任何权限";
                //}
                //lblUsers.Text = strUsers;
            }
        }

        /// <summary>
        /// 将字符串数组转成字符串
        /// </summary>
        /// <param name="strArray">字符串数组</param>        
        /// <param name="Separator">分隔符</param>
        /// <returns>字符串</returns>
        public static string StringArrayToString(string[] strArray, char Separator)
        {
            string str = string.Empty;
            for (int i = 0; i < strArray.Length; i++)
            {
                str = str + strArray[i] + Separator;
            }
            return str.Substring(0, str.Length - 1);
        }

        protected void Btn_ok_Click(object sender, EventArgs e)
        {
            using (var entity = new QuaManSysEntities())
            {
                SysRole aRole = (from q in entity.SysRole
                            where q.Id == lblId.Text
                            select q).Single();
                aRole.Name = txtName.Text;
                aRole.Description = txtDescription.Text;
                entity.SaveChanges();
            }
            strinfo.Visible = true;
            pnlRoleDetail.Visible = false;
        }
    }
}